package com.exam.controller;

import com.exam.entity.Question;
import com.exam.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class QuestionController {

    @Autowired
    QuestionService questionService;

    @RequestMapping("/index")
    public String index(Model model,Integer subject){
        List<Question> questions = questionService.getByQuery(subject);
        model.addAttribute("questions",questions);
        return "list";
    }

    @GetMapping("/addpre")
    public String addpre(){
        return "add";
    }

    @PostMapping("/add")
    public String add(Question question){
        System.out.println(question);
        questionService.add(question);
        return "redirect:index";
    }

    @RequestMapping("/delete")
    public String delete(Long questionid){
        questionService.delete(questionid);
        return "redirect:index";
    }

    @RequestMapping("/updatepre")
    public String updatepre(Long questionid,Model model){
        model.addAttribute("question",questionService.getById(questionid));
        return "update";
    }

    @RequestMapping("/update")
    public String update(Question question){
        questionService.update(question);
        return "redirect:index";
    }
}
